"use strict";(function(){if(!window.BX){window.BX={}}else if(window.BX.RestClient){return}var e=window.BX;e.RestClient=function(e){e=e||{};this.endpoint=e.endpoint||"/rest";this.queryParams=e.queryParams||"";this.cors=e.cors===true};e.RestClient.prototype.callMethod=function(e,t,r,i,s){return n({method:e,data:t,callback:r,sendCallback:i,logTag:s,endpoint:this.endpoint,queryParams:this.queryParams,cors:this.cors})};e.RestClient.prototype.callBatch=function(e,t,i,s,o){var a=r.isArray(e)?[]:{};var u=0;var f=function(e){n.batch(e,t,i,s,this.endpoint,this.queryParams,this.cors,o)}.bind(this);for(var l in e){var c=null,p=null;if(!!e[l]&&e.hasOwnProperty(l)){if(r.isArray(e[l])){c=e[l][0];p=e[l][1]}else if(!!e[l].method){c=e[l].method;p=e[l].params}if(!!c){u++;a[l]=[c,p]}}}if(u>0){var d=function(e){return function(t){a[e]=a[e][0]+"?"+t;if(--u<=0)f(a)}};for(var h in a){if(a.hasOwnProperty(h)){n.prepareData(a[h][1],"",d(h))}}}};e.RestClient.prototype.setEndpoint=function(e){this.endpoint=e};e.RestClient.prototype.enableCorsRequest=function(e){this.cors=e===true};e.RestClient.prototype.setQueryParams=function(e){this.queryParams=e};if(typeof e.namespace!=="undefined"){var t=new e.RestClient;if(typeof e.rest=="undefined"){e.rest={}}e.rest.callMethod=function(e,r,n,i,s){return t.callMethod(e,r,n,i,s)};e.rest.callBatch=function(e,r,n,i,s){return t.callBatch(e,r,n,i,s)}}var r={isArray:function(e){return e&&Object.prototype.toString.call(e)=="[object Array]"},isFunction:function(e){return e===null?false:typeof e=="function"||e instanceof Function},isString:function(e){return e===""?true:e?typeof e=="string"||e instanceof String:false},isDomNode:function(e){return e&&typeof e=="object"&&"nodeType"in e},isDate:function(e){return e&&Object.prototype.toString.call(e)=="[object Date]"},buildQueryString:function(e){var t="";for(var r in e){if(!e.hasOwnProperty(r)){continue}var n=e[r];if(this.isArray(n)){n.forEach(function(e,n){t+=encodeURIComponent(r+"["+n+"]")+"="+encodeURIComponent(e)+"&"})}else{t+=encodeURIComponent(r)+"="+encodeURIComponent(n)+"&"}}if(t.length>0){t=t.substr(0,t.length-1)}return t},clone:function(e,t){var r,n,i;if(t!==false)t=true;if(e===null)return null;if(this.isDomNode(e)){r=e.cloneNode(t)}else if(typeof e=="object"){if(this.isArray(e)){r=[];for(n=0,i=e.length;n<i;n++){if(typeof e[n]=="object"&&t)r[n]=this.clone(e[n],t);else r[n]=e[n]}}else{r={};if(e.constructor){if(this.isDate(e))r=new Date(e);else r=new e.constructor}for(n in e){if(typeof e[n]=="object"&&t)r[n]=this.clone(e[n],t);else r[n]=e[n]}}}else{r=e}return r}};var n=function(t){var s=!!t.callback&&r.isFunction(t.callback);var o=typeof e.Promise==="undefined"||s?null:new e.Promise;var a=t.sendCallback||function(){};var u=t.withoutRestoringCsrf||false;var f=t.loginAttempt||0;var l=n.xhr();var c=t.endpoint+"/"+n.escape(t.method)+".json"+(t.logTag?"?logTag="+t.logTag:"");l.open("POST",c);l.setRequestHeader("Content-Type","application/x-www-form-urlencoded");if(t.cors){l.withCredentials=true}var p=false;l.onprogress=function(){};l.ontimeout=function(){};l.timeout=0;l.onload=function(){if(p)return;l.onload=function(){};var r=n.isSuccess(l);var a=l.status;if(r){var c=l.responseText;if(c.length>0){try{c=JSON.parse(c)}catch(e){r=false}}if(a==401){if(c.extended_error==="user_not_authorized"&&f===0){if(c.sessid){console.warn("BX.rest: csrf-token has expired, replace to a new token");e.message({bitrix_sessid:c.sessid})}if("BXDesktopSystem"in window){console.warn("BX.rest: you are not authorized, trying to log in");t.loginAttempt=1;BXDesktopSystem.Login({success:function(){console.warn("BX.rest: successfully logged in, repeating request");if(!s){t.callback=function(e){if(e.error()){o.reject(e)}else{o.fulfill(e)}}}n(t)}});return true}}else if(c.sessid&&!u){e.message({bitrix_sessid:c.sessid});console.warn("BX.rest: your csrf-token has expired, send query with a new token");t.withoutRestoringCsrf=true;if(!s){t.callback=function(e){if(e.error()){o.reject(e)}else{o.fulfill(e)}}}n(t);return true}}else if(a==0){c={result:{},error:"ERROR_NETWORK",error_description:"A network error occurred while the request was being executed."}}else{if(a==200){if(c.length<=0){c={result:{},error:"BLANK_ANSWER",error_description:"Empty answer with correct http code, network error possible."}}}else if(c.length<=0){c={result:{},error:"BLANK_ANSWER_WITH_ERROR_CODE",error_description:"Empty answer with error http code: "+a}}}}l=null;if(r){var d=new i(c,t,a);if(s){t.callback.apply(window,[d])}else{if(d.error()){o.reject(d)}else{o.fulfill(d)}}}else{var d=new i({error:"ERROR_UNEXPECTED_ANSWER",error_description:"Server returned an unexpected response.",ex:{}},t,0);if(s){t.callback.apply(window,[d])}else{o.reject(d)}}};l.onerror=function(e){var r=new i({error:"ERROR_NETWORK",error_description:"A network error occurred while the request was being executed.",ex:e},t,0);if(s){t.callback.apply(window,[r])}else{o.reject(r)}};var d="";if(t.queryParams){d=r.buildQueryString(t.queryParams)}else if(typeof e.bitrix_sessid!=="undefined"){d="sessid="+e.bitrix_sessid()}if(typeof t.start!=="undefined"){d+="&start="+parseInt(t.start)}if(!!t.data){n.prepareData(t.data,"",function(e){d+="&"+e;l.send(d);a(l)})}else{l.send(d);a(l)}return s||!o?l:o};n.batch=function(e,t,s,o,a,u,f,l){return n({method:"batch",data:{halt:!!s?1:0,cmd:e},callback:function(n,s,o){if(!t){return false}var l=n.error();var c=n.data();var p=r.isArray(e)?[]:{};for(var d in e){if(!!e[d]&&e.hasOwnProperty(d)){if(r.isString(e[d])){var h=e[d].split("?")}else{h=[r.isArray(e[d])?e[d][0]:e[d].method,r.isArray(e[d])?e[d][1]:e[d].data]}if(c&&typeof c.result!=="undefined"&&(typeof c.result[d]!=="undefined"||typeof c.result_error[d]!=="undefined")){p[d]=new i({result:typeof c.result[d]!=="undefined"?c.result[d]:{},error:c.result_error[d]||undefined,total:c.result_total[d],time:c.result_time[d],next:c.result_next[d]},{method:h[0],data:h[1],callback:t,endpoint:a,queryParams:u,cors:f},n.status)}else if(l){p[d]=new i({result:{},error:l.ex,total:0},{method:h[0],data:h[1],callback:t,endpoint:a,queryParams:u,cors:f},n.status)}}}t.apply(window,[p])},sendCallback:o,endpoint:a,queryParams:u,cors:f,logTag:l})};n.xhr=function(){return new XMLHttpRequest};n.escape=function(e){return encodeURIComponent(e)};n.prepareData=function(e,t,i){var s="",o=[];if(r.isString(e)||e===null){i.call(document,e||"")}else{for(var u in e){if(!e.hasOwnProperty(u)){continue}var f=n.escape(u);if(t)f=t+"["+f+"]";if(typeof e[u]==="object"){if(r.isArray(e[u])&&e[u].length<=0){continue}o.push([f,e[u]])}else{if(s.length>0){s+="&"}if(typeof e[u]==="boolean"){s+=f+"="+(e[u]?1:0)}else{s+=f+"="+n.escape(e[u])}}}var l=o.length;if(l>0){var c=function(e){s+=(!!e?"&":"")+e;if(--l<=0){i.call(document,s)}};var p=l;for(var u=0;u<p;u++){if(r.isDomNode(o[u][1])){if(o[u][1].tagName.toUpperCase()==="INPUT"&&o[u][1].type==="file"){if(a.canUse()){a(o[u][1],function(e){return function(t){if(r.isArray(t)&&t.length>0){c(e+"[0]="+n.escape(t[0])+"&"+e+"[1]="+n.escape(t[1]))}else{c(e+"=")}}}(o[u][0]))}}else if(typeof o[u][1].value!=="undefined"){c(o[u][0]+"="+n.escape(o[u][1].value))}else{c("")}}else if(r.isDate(o[u][1])){c(o[u][0]+"="+n.escape(o[u][1].toJSON()))}else if(r.isArray(o[u][1])&&o[u][1].length<=0){c(o[u][0]+"=")}else{n.prepareData(o[u][1],o[u][0],c)}}}else{i.call(document,s)}}};n.isSuccess=function(e){return typeof e.status==="undefined"||e.status>=200&&e.status<300||e.status===304||e.status>=400&&e.status<500||e.status===1223||e.status===0};var i=function(e,t,n){this.answer=e;this.query=r.clone(t);this.status=n;if(typeof this.answer.next!=="undefined"){this.answer.next=parseInt(this.answer.next)}if(typeof this.answer.error!=="undefined"){this.answer.ex=new s(this.status,typeof this.answer.error==="string"?this.answer:this.answer.error)}};i.prototype.data=function(){return this.answer.result};i.prototype.time=function(){return this.answer.time};i.prototype.error=function(){return this.answer.ex};i.prototype.error_description=function(){return this.answer.error_description};i.prototype.more=function(){return!isNaN(this.answer.next)};i.prototype.total=function(){return parseInt(this.answer.total)};i.prototype.next=function(e){if(this.more()){this.query.start=this.answer.next;if(!!e&&r.isFunction(e)){this.query.callback=e}return n(this.query)}return false};var s=function(e,t){this.status=e;this.ex=t};s.prototype.getError=function(){return this.ex};s.prototype.getStatus=function(){return this.status};s.prototype.toString=function(){return this.ex.error+(!!this.ex.error_description?": "+this.ex.error_description:"")+" ("+this.status+")"};var o=function(e){var t=new Uint8Array(e);var r="";if(typeof t.forEach==="function"){t.forEach(function(e){r+=String.fromCharCode(e)})}else{var n=t.length;for(var i=0;i<n;i+=1){r+=String.fromCharCode(t[i])}}return btoa(r)};var a=function(e,t){if(a.canUse()){var r=e.files,n=0,i=e.multiple?[]:null;for(var s=0,u;u=r[s];s++){var f=new window.FileReader;f.BXFILENAME=r[s].name;f.onload=function(e){e=e||window.event;var r=[this.BXFILENAME,o(e.target.result)];if(i===null)i=r;else i.push(r);if(--n<=0){t(i)}};f.readAsArrayBuffer(u)}n=s;if(n<=0){t(i)}}};a.canUse=function(){return!!window.FileReader}})();
//# sourceMappingURL=rest.client.map.js